<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>搜索功能测试</title>
</head>
<body>
    <h1>搜索功能测试</h1>
    <div>
        <input type="text" id="searchInput" placeholder="输入搜索内容" value="知识库">
        <button onclick="testSearch()">搜索</button>
        <button onclick="clearResults()">清空结果</button>
    </div>
    <div id="results"></div>

    <script>
        async function testSearch() {
            const query = document.getElementById('searchInput').value;
            const resultsDiv = document.getElementById('results');
            
            try {
                resultsDiv.innerHTML = '<p>搜索中...</p>';
                
                const response = await fetch(`http://localhost:8080/api/knowledge/search?query=${encodeURIComponent(query)}&maxResults=10&minScore=0.8`);
                
                if (response.ok) {
                    const data = await response.json();
                    console.log('搜索结果:', data);
                    
                    if (data.length === 0) {
                        resultsDiv.innerHTML = '<p style="color: red;">未找到结果</p>';
                    } else {
                        let html = `<h3>找到 ${data.length} 个结果:</h3>`;
                        data.forEach((result, index) => {
                            html += `
                                <div style="border: 1px solid #ccc; margin: 10px 0; padding: 10px;">
                                    <h4>结果 ${index + 1}</h4>
                                    <p><strong>内容:</strong> ${result.content}</p>
                                    <p><strong>相似度:</strong> ${(result.similarity * 100).toFixed(1)}%</p>
                                    <p><strong>来源:</strong> ${result.source}</p>
                                    <p><strong>文档ID:</strong> ${result.documentId}</p>
                                </div>
                            `;
                        });
                        resultsDiv.innerHTML = html;
                    }
                } else {
                    const errorText = await response.text();
                    resultsDiv.innerHTML = `<p style="color: red;">搜索失败: ${response.status} - ${errorText}</p>`;
                }
            } catch (error) {
                resultsDiv.innerHTML = `<p style="color: red;">请求失败: ${error.message}</p>`;
            }
        }
        
        function clearResults() {
            document.getElementById('results').innerHTML = '';
        }
        
        // 页面加载时自动搜索
        window.onload = function() {
            setTimeout(testSearch, 1000);
        };
    </script>
</body>
</html> 